package com.rttmall.shopbackend.app.service.impl;

import java.util.Date;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.rttmall.shopbackend.app.gambling.mapper.ProductMapper;
import com.rttmall.shopbackend.app.gambling.pojo.Product;
import com.rttmall.shopbackend.app.service.OssService;
import com.rttmall.shopbackend.app.service.UploadImageService;
import com.rttmall.shopbackend.utils.DateUtil;
import com.rttmall.shopbackend.utils.FileUtil;
import com.rttmall.shopbackend.utils.RandomUtil;

@Service
public class UploadImageServiceImpl implements UploadImageService {

	@Value("${image.folder.format}")
	private String format;
	@Value("${image.folder}")
	private String folder;
    @Value("${product.image.tempPath}")
    private String tempPath;
	
	@Autowired
	private OssService ossService;
	@Autowired
	private ProductMapper productMapper;
	
	/**
	 * KindEditor图片上传
	 */
	@Override
	public String kindEditorUploadImage(MultipartHttpServletRequest multipartRequest) throws Exception{
		String filePath = "";
		Iterator item = multipartRequest.getFileNames();
		while (item.hasNext()) {
			MultipartFile file = multipartRequest.getFile((String) item.next());
			String fileName = file.getOriginalFilename();
			file.getOriginalFilename();
			String extend = FileUtil.getExtend(fileName);// 获取文件扩展名
			String sonPath = DateUtil.format(new Date(), format);// 子文件夹
			String imageName = RandomUtil.getRandomString(6);// 文件命名
			filePath = folder + sonPath + "/" + imageName + "." + extend;
			ossService.putObject(filePath, file.getInputStream());
		}
		return filePath;
	}
	/**
	 * 商品主图上传
	 */
	@Override
	public void productUploadImg(MultipartHttpServletRequest multiRequest,
			Product product) throws Exception {
		if (null != multiRequest) {
			Iterator<String> iter = multiRequest.getFileNames();
			String filePath = "";
			while (iter.hasNext()) {
				MultipartFile file = multiRequest.getFile(iter.next());
				String fileName = file.getOriginalFilename();// 获取文件名
				if (StringUtils.isEmpty(fileName)) {
					continue;
				}
				String extend = FileUtil.getExtend(fileName);// 获取文件扩展名
				String sonPath = DateUtil.format(new Date(), format);// 子文件夹
				String imageName = RandomUtil.getRandomString(6);// 文件命名
				filePath = folder + sonPath + "/" + imageName + "." + extend;
				ossService.putObject(filePath, file.getInputStream());
				//上传成功后删除原有的图片
				if(!StringUtils.isEmpty(product.getId())){
					Product product2 = productMapper.selectByPrimaryKey(product.getId());
					if(!StringUtils.isEmpty(product2.getImage())){
						ossService.deleteObject(product2.getImage());
					}
				}
				if(null != product){
					product.setImage(filePath);
				}
			}
		}
	}
}
